C++ 알고리즘에 도움되는 함수

마지막 수정일: 2025. 09. 24.

C++
inlcude <bits/stdc++.h>

//javascript map와 유사
vector<int> v
transform(v.begin(), v.end(), av.begin(), [](int x){return x*2;})
//javascript filter와 유사, 메모리 주소 그대로에 들어가야해서 그만큼 메모리를 할당을 미리 하거나 back_inserter로 넣어야 됨
copy_if(v.begin(), v.end(), std::back_inserter(evens), [](int x) { return x % 2 == 0; });

count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
//iterable 반환, 값 접근하려면 *써야됨
max_element(nums.begin(), [](const Person& a, const Person& b) {
    return a.age < b.age;  // 나이가 더 큰 사람을 찾는다
}))

//map to vector
vector<pair<string, int>> v(m.begin(), m.end());


//보통 sort는 이러면 오름차순으로 작은 게 먼저지만 pq에선 큰 게 top, 먼저 pop되는 애를 생각하면 될 듯
struct cmp{
	bool operator()(Student a, Student b){
		return a.priority < b.priority;
	}
};
priority_queue<Student, vector<Student>, cmp> q;

//문자, stirng하려면 transform에
toupper(c)
tolower(c)

to_string(int n)
itos(int n)